﻿$(document).ready(function () {
    $(".tbl-selected tbody tr").click(function () {
        $(this).addClass("row-selected");
        $(".tbl-selected tbody tr").not(this).removeClass('row-selected');
    });

    $('.fr-saverdmorder').submit(function () {
        var serializedForm = $(this).serialize();
        $(".fr-saverdmorder").hide();
        $(".fr-saverdmorder-loading").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            type: 'POST',
            url: '/rdm/saverdmorder',
            data: serializedForm,
            success: function (data) {
                $(".fr-saverdmorder-loading").hide();
                $(".fr-saverdmorder").show();
                alert('Successfully!');
            },
            error: function () {
                alert('Save fail!');
            }
        });
        return false;
    });

    $('.fr-createorderitem').submit(function () {
        var serializedForm = $(this).serialize();
        $(".pn-orderitem").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            type: 'POST',
            url: '/rdm/createorderitem',
            data: serializedForm,
            success: function (data) {
                $(".pn-orderitem").html(data);
                bindjs();
            },
            error: function () {
                alert('create fail!');
            }
        });
        return false;
    });

    $('.fr-saveorderitem').submit(function () {
        var serializedForm = $(this).serialize();
        $(".fr-saveorderitem").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            type: 'POST',
            url: '/rdm/saveorderitem',
            data: serializedForm,
            success: function (data) {
                $(".fr-saveorderitem").html(data);
                alert('Successfully!');
            },
            error: function () {
                alert('Save fail!');
            }
        });
        return false;
    });

    $('.fr-collectiondetail').submit(function () {
        if ($("#CarrierID").val() == 0) {
            alert("You must select a Courier before you can save a collection.");
            $("#CarrierID").focus();
            return false;
        }
        if ($("#NumberOfParcels").val() == 0) {
            alert("You need to have 1 or more parcels.");
            $("#NumberOfParcels").focus();
            return false;
        }
        var serializedForm = $(this).serialize();
        $(".fr-collectiondetail").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            type: 'POST',
            url: '/rdm/savecollection',
            cache: false,
            data: serializedForm,
            success: function (data) {
                $(".fr-collectiondetail").html(data);
                $('#datetimepicker2').datetimepicker({
                    language: 'en',
                    pick12HourFormat: true
                });
            },
            error: function () {
                alert('Save fail!');
            }
        });
        return false;
    });

    $('.fr-rejectiondetail').submit(function () {
        if ($("#RejectionActionID").val() == 0) {
            alert("Please select an Action");
            $("#RejectionActionID").focus();
            return false;
        }
        var serializedForm = $(this).serialize();
        $(".fr-rejectiondetail").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            type: 'POST',
            url: '/rdm/saverejectedrdm',
            data: serializedForm,
            success: function (data) {
                $(".fr-rejectiondetail").html(data);
            },
            error: function () {
                alert('Save fail!');
            }
        });
        return false;
    });

    $('.fr-replacementdetail').submit(function () {
        var serializedForm = $(this).serialize();
        $(".fr-replacementdetail").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            type: 'POST',
            url: '/rdm/savereplacement',
            data: serializedForm,
            success: function (data) {
                $(".fr-replacementdetail").html(data);
                $('.datetimepicker').datetimepicker({
                    language: 'en',
                    pick12HourFormat: true
                });
            },
            error: function () {
                alert('Save fail!');
            }
        });
        return false;
    });

    $('.fr-parcel').submit(function () {
        var serializedForm = $(this).serialize();
        App.blockUI($('.fr-parcel'), false);
        $.ajax({
            type: 'POST',
            url: '/rdm/saveparcel',
            data: serializedForm,
            success: function (data) {
                $(".fr-parcel").html(data);
                $('.datetimepicker').datetimepicker({
                    language: 'en',
                    pick12HourFormat: true
                });
            },
            error: function () {
                alert('Save fail!');
            }
        });
        return false;
    });

    $('.fr-editimage').submit(function () {
        var serializedForm = $(this).serialize();
        $(".tbl-photodispute").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            type: 'POST',
            url: '/rdm/editimage',
            data: serializedForm,
            success: function (data) {
                $(".close").trigger("click");
                $(".tbl-photodispute").html(data);
                $("#txt-imagetext").val("");
                $("#txt-imagetext").val("");
                alert("Success!");
            },
            error: function () {
                alert('Save fail!');
            }
        });
        return false;
    });
    $('.fr-editdocument').submit(function () {
        var serializedForm = $(this).serialize();
        $(".tbl-documentdispute").html("<div class='ajax-loadForm'></div>");
        $.ajax({
            type: 'POST',
            url: '/rdm/editdocument',
            data: serializedForm,
            success: function (data) {
                $(".close").trigger("click");
                $(".tbl-documentdispute").html(data);
                $("#txt-documenttext").val("");
                alert("Success!");
            },
            error: function () {
                alert('Save fail!');
            }
        });
        return false;
    });

    $("#fr-AddPhoto").submit(function () {
        App.blockUI($('#fr-AddPhoto'), false);
        //grab all form data  
        var formData = new FormData($("#fr-AddPhoto")[0]);
        $.ajax({
            url: '/rdm/addphoto',
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (data) {
                $('.tbl-photodispute table tbody').append(data);
                $('#fr-AddPhoto').reset();
                App.unblockUI($('#fr-AddPhoto'), false);
                $('#addPhoto').modal('hide');
            },
            error: function (error) {
                App.unblockUI($('#fr-AddPhoto'), false);
            }
        });
        return false;
    });
    $("#fr-AddDocument").submit(function () {
        App.blockUI($('#fr-AddDocument'), false);
        //grab all form data  
        var formData = new FormData($("#fr-AddDocument")[0]);
        $.ajax({
            url: '/rdm/adddocument',
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (data) {
                $('.tbl-documentdispute table tbody').append(data);
                $('#fr-AddDocument').reset();
                App.unblockUI($('#fr-AddDocument'), false);
                $('#addDocument').modal('hide');
            },
            error: function (error) {
                App.unblockUI($('#fr-AddDocument'), false);
            }
        });
        return false;
    });
    $("#IsFullDisputeItems").change(function () {
        if ($(this).is(':checked')) {
            $("#DisputeItems").html($("#hfDisputeItems").val());
        } else {
            $("#DisputeItems").html("");
        }
    });

    $("#IsFulDisputeReplacementItems").change(function () {
        if ($(this).is(':checked')) {
            $("#DisputeReplacementItems").html($("#hfDisputeReplacementItems").val());
        } else {
            $("#DisputeReplacementItems").html("");
        }
    });


});

function bindjs() {
    $(".tbl-selected tbody tr").click(function () {
        $(this).addClass("row-selected");
        $(".tbl-selected tbody tr").not(this).removeClass('row-selected');
    });

}

function getrdmdetail(disputeid) {
    App.blockUI($('.rdm-detail'), false);
    //$(".rdm-detail").html("<div class='ajax-loading'></div>");
    $.ajax({
        url: '/rdm/getapproverdm',
        data: { disputeid: disputeid },
        success: function (data) {
            $(".rdm-detail").html(data);
            $('.fr-approverdm').submit(function () {
                var serializedForm = $(this).serialize();
                App.blockUI($('.rdm-detail'), false);
                //$(".rdm-detail").html("<div class='ajax-loading'></div>");
                $.ajax({
                    type: 'POST',
                    url: '/rdm/finaliserdm',
                    data: serializedForm,
                    success: function (data) {
                        if (!isNaN(data)) {
                            window.location = "/rdm/newrdmorder/" + data;
                        } else {
                            $(".rdm-detail").html(data);
                        }
                    },
                    error: function () {
                        alert('Finalise error!');
                    }
                });
                return false;
            });
        }
    });
}

function createrdmorder(disputeid) {
    App.blockUI($('.createrdmorder-loading'), false);
    $.ajax({
        url: '/rdm/createrdmorder',
        data: { id: disputeid },
        success: function (data) {
            $("#orderref").val(data.orderref);
            $("#specialinstruction").val(data.specialinstruction);
            $("#orderid").val(data.id);
            $("#hforderid").val(data.id);
            $("#btn-selectdate-rdmorder").attr("onclick", "PopupCenter('/orderview/NewRdmOrderDeliveryDate?orderid=" + data.id + "', 1000, 580)");
            
            $(".btn-showrdmorder").attr("href", "/orderview/orderdetail?orderref=" + data.orderref);
            $(".createrdmorder-loading").remove();
            $(".createrdmorder").show("fast");
            $(".btn-createrdmorder").attr('disabled', 'disabled');
        },
        error: function () {
            $(".createrdmorder-loading").html("Create RDM Order fail");
        }
    });
}

function orderitemselect(orderitemid) {
    App.blockUI($('.pn-orderitempart'), false);
    $.ajax({
        url: '/rdm/getorderitempart',
        data: { orderitemid: orderitemid },
        success: function (data) {
            $(".pn-orderitempart").html(data);
            App.blockUI($('.fr-saveorderitem'), false);
            $.ajax({
                url: '/rdm/getorderitem',
                data: { orderitemid: orderitemid },
                success: function (data) {
                    $(".fr-saveorderitem").html(data);
                }
            });
            var disputeid = $('#hfdisputeid').val();
            var popupedit = "PopupCenter('/orderview/editpartsinorderitem?orderitemid=" + orderitemid + "&disputeid=" + disputeid + "', 1000, 580)";
            var popupallocate = "PopupCenter('/orderview/addpartsinorderitem?orderitemid=" + orderitemid + "&disputeid=" + disputeid + "', 1000, 580)";
            $(".btn-editpart").attr('onclick', popupedit);
            $(".btn-allocateparts").attr('onclick', popupallocate);
        }
    });
}

function updatelayerorderitempart(orderitemid) {
    $(".pn-orderitempart").html("<div class='ajax-loading'></div>");
    $.ajax({
        url: '/rdm/getorderitempart',
        data: { orderitemid: orderitemid },
        success: function (data) {
            $(".pn-orderitempart").html(data);
        }
    });
}

function collectionselected(disputeId) {
    App.blockUI($('.fr-collectiondetail'), false);
    $.ajax({
        url: '/rdm/collectiondetails',
        data: { disputeId: disputeId },
        success: function (data) {
            $(".fr-collectiondetail").html(data);
            $('#datetimepicker2').datetimepicker({
                language: 'en',
                pick12HourFormat: true
            });
        },
        error: function () {
            alert('load fail!');
        }
    });
}

function rejectionselected(disputeId) {
    App.blockUI($('.fr-rejectiondetail'), false);
    $.ajax({
        url: '/rdm/rejectedrdmdetails',
        data: { disputeId: disputeId },
        success: function (data) {
            App.unblockUI($('.fr-rejectiondetail'));
            $(".fr-rejectiondetail").html(data);
        },
        error: function () {
            alert('load fail!');
        }
    });
}
function replacementselected(disputeid) {
    App.blockUI($('.fr-replacementdetail'), false);
    $.ajax({
        url: '/rdm/replacementdetails',
        data: { disputeid: disputeid },
        success: function (data) {
            $(".fr-replacementdetail").html(data);
            $('.datetimepicker').datetimepicker({
                language: 'en',
                pick12HourFormat: true
            });
        },
        error: function () {
            alert('load fail!');
        }
    });
}
function returnselected(disputeid) {
    App.blockUI($('.return-details'), false);
    $.ajax({
        url: '/rdm/returndetails',
        data: { disputeid: disputeid },
        success: function (data) {
            $(".return-details").html(data);
            bindjs();
        },
        error: function () {
            alert('load fail!');
        }
    });
}

function parcelselected(parcelid, disputeid) {
    App.blockUI($('.fr-parcel'), false);
    $(".btn-removeparcel").attr('onclick', 'removeparcel(' + parcelid + ', ' + disputeid + ')');
    $.ajax({
        url: '/rdm/parceldetail',
        data: { parcelid: parcelid },
        success: function (data) {
            $(".fr-parcel").html(data);
        },
        error: function () {
            alert('load fail!');
        }
    });
}

function removeparcel(parcelid, disputeid) {
    $.ajax({
        url: '/rdm/removeparcel',
        data: { id: parcelid },
        success: function (data) {
            returnselected(disputeid);
            $(".btn-saveparcel").attr('type', 'button');
        },
        error: function () {
            alert('load fail!');
        }
    });
}

function updatelistimagedispute(disputeid) {
    $(".tbl-photodispute").html("<div class='ajax-loading'></div>");
    $.ajax({
        url: "/rdm/PhotosDispute",
        data: { disputeid: disputeid },
        success: function (data) {
            $(".tbl-photodispute").html(data);
        },
        error: function () {
            alert('Save fail!');
        }
    });
}

function inspectionform(disputeid) {
    App.blockUI($('.return-details'), false);
    $.ajax({
        url: '/rdm/inspectionform',
        data: { disputeid: disputeid },
        success: function (data) {
            $(".return-details").html(data);
            bindjs();
        },
        error: function () {
            alert('load fail!');
        }
    });
}

function deletephoto(id, disputeid) {
    $(".tbl-photodispute").html("<div class='ajax-loading'></div>");
    $.ajax({
        url: "/rdm/deletephoto",
        data: { id: id, disputeid: disputeid },
        success: function (data) {
            $(".tbl-photodispute").html(data);
            alert('Success!');
        },
        error: function () {
            alert('Delete fail!');
        }
    });
}

function editimage(photoid, disputeid, action) {
    $("#hf-photoid").val(photoid);
    $("#hf-disputeid").val(disputeid);
    if (action == "description") {
        $(".lbimagetitle").html("New Description");
        $("#txt-imagetext").attr('name', 'description');
    } else {
        $(".lbimagetitle").html("New Name");
        $("#txt-imagetext").attr('name', 'name');
    }
    $("#btn-editimage").trigger("click");
}

function updatelistdocumentdispute(disputeid) {
    $(".tbl-document").html("<div class='ajax-loading'></div>");
    $.ajax({
        url: "/rdm/DocumentDispute",
        data: { disputeid: disputeid },
        success: function (data) {
            $(".tbl-document").html(data);
        },
        error: function () {
            alert('Load fail!');
        }
    });
}


function deletedocument(id, disputeid) {
    $(".tbl-documentdispute").html("<div class='ajax-loading'></div>");
    $.ajax({
        url: "/rdm/deletedocument",
        data: { id: id, disputeid: disputeid },
        success: function (data) {
            $(".tbl-documentdispute").html(data);
            alert('Success!');
        },
        error: function () {
            alert('Delete fail!');
        }
    });
}

function editdocument(documentid, disputeid, action) {
    $("#hf-documentid").val(documentid);
    $("#hfd-disputeid").val(disputeid);
    if (action == "description") {
        $(".lbdocumenttitle").html("New Description");
        $("#txt-documenttext").attr('name', 'description');
    } else {
        $(".lbdocumenttitle").html("New Name");
        $("#txt-documenttext").attr('name', 'name');
    }
    $("#btn-editdocument").trigger("click");
}
function resolutionchange(resolution) {
    if (resolution == 1 || resolution == 2 || resolution == 3 || resolution == 4) {
        alert("You have selected a resolution that requires a collection.\r\nPlease ask the customer to provide a preferred collection date.");
    }
    if (resolution == 6) {
        $("#DisputeItems").html($("#hfDisputeItems").val());
        $("#IsFullDisputeItems").prop("checked", true);

        $("#DisputeReplacementItems").html("");
        $("#IsFulDisputeReplacementItems").prop("checked", false);
        $("#DisputeReplacementItems").attr("disabled", "disabled");
        $("#IsFulDisputeReplacementItems").attr("disabled", "disabled");
    } else if (resolution == 3 || resolution == 6 || resolution == 11 || resolution == 4) {
        $("#DisputeReplacementItems").html("");
        $("#IsFulDisputeReplacementItems").prop("checked", false);
        $("#DisputeReplacementItems").attr("disabled", "disabled");
        $("#IsFulDisputeReplacementItems").attr("disabled", "disabled");
    } else {
        $("#DisputeItems").html("");
        $("#DisputeReplacementItems").html("");
        $("#IsFullDisputeItems").prop("checked", false);
        $("#IsFulDisputeReplacementItems").prop("checked", false);

        $("#DisputeReplacementItems").removeAttr("disabled");
        $("#IsFulDisputeReplacementItems").removeAttr("disabled");
    }
    if (resolution == 4 || resolution == 5) {
        $("#datetimepicker2 input").attr("disabled", "disabled");
    } else {
        $("#datetimepicker2 input").removeAttr("disabled");
    }
}
function ViewRDMQuestionnaire(questionaireId) {
    $.ajax({
        url: "/rdm/QuestionnaireIsFac51CheckList",
        method: "GET",
        data: { questionaireId: questionaireId },
        success: function (data) {
            if (data == "True") {
                PopupCenter('/rdm/questionnaireform/' + questionaireId + '?orderref=' + $('#OrderRef').val(), 800, 490);
            }
            else if (data == "False") {
                PopupCenter('/rdm/questionnairenonfac/' + questionaireId + '?orderref=' + $('#OrderRef').val(), 800, 490);
            } else alert('No questionnaire');
        }, error: function () {
            alert("error!");
        }
    });
}

function RdmOrderItemPartDetail(orderItemId,disputeid,divresult) {
    App.blockUI($('#ItemPart'));
    if (!divresult.hasClass('IsLoaded')) {
        $.ajax({
            url: "/rdm/RdmOrderItemPartDetail",
            method: "GET",
            data: { orderItemId: orderItemId, disputeid: disputeid },
            success: function (data) {
                divresult.html(data);
                divresult.addClass('IsLoaded');
                App.unblockUI($('#ItemPart'));
            },
            error: function () {
                alert("error!");
                App.unblockUI($('#ItemPart'));

            }
        });
    } else App.unblockUI($('#ItemPart'));
}

function approveactionchange(actionid) {
    if (actionid == 5 || actionid == 6 || actionid == 7 || actionid == 8) {
        $("#DisputeItems").attr("readonly", "readonly");
    } else {
        $("#DisputeItems").removeAttr("readonly");
    }
    if (actionid == 3 || actionid == 6 || actionid == 8 || actionid == 11) {
        $("#DisputeReplacementItems").attr("readonly", "readonly");
    }
        //enable the replacement items
    else {
        $("#DisputeReplacementItems").removeAttr("readonly");
    }
}
function CheckFinaliseRdm(disputeid) {
    App.blockUI($("#frsavedispute"));
        $.ajax({
            url: "/rdm/CheckFinaliseRdm",
            method: "GET",
            data: { disputeid: disputeid },
            success: function (msg) {
                if (msg != "") {
                    if (!confirm(msg)) {
                        App.unblockUI($("#frsavedispute"));
                        return;
                    }
                }
                $("#frsavedispute").submit();
            }
        });
    }
